package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.exception.CustomException;
import com.example.mapper.*;
import com.example.pojo.entity.*;
import com.example.service.IInventoryService;
import com.example.service.IOutboundService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物资出库 服务实现类
 *
 * @author wy
 * @since 2024-07-17
 */
@Service
public class OutboundServiceImpl extends ServiceImpl<OutboundMapper, Outbound> implements IOutboundService {

    @Autowired
    private OutboundMapper outboundMapper;

    @Autowired
    private IInventoryService iInventoryService;


    @Override
    public Page<Outbound> selectPage(Outbound outbound, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<Outbound> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Outbound.class)
                .selectAs(Warehouse::getName,Outbound::getWarehouseName)
                .selectAs(Material::getName,Outbound::getMaterialName)
                .selectAs(Project::getName,Outbound::getProjectName)
                .innerJoin(Warehouse.class,Warehouse::getId,Outbound::getWarehouseId)
                .innerJoin(Material.class,Material::getId,Outbound::getMaterialId)
                .innerJoin((Project.class),Project::getId,Outbound::getProjectId)
                .eq(ObjectUtil.isNotNull(outbound.getWarehouseId()), Outbound::getWarehouseId, outbound.getWarehouseId())
                .eq(ObjectUtil.isNotNull(outbound.getMaterialId()), Outbound::getMaterialId, outbound.getMaterialId())
                .eq(ObjectUtil.isNotNull(outbound.getProjectId()),Outbound::getProjectId,outbound.getProjectId());

        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }

    @Override
    public void add(Outbound outbound) {
        MPJLambdaWrapper<Inventory> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .eq(Inventory::getMaterialId,outbound.getMaterialId())
                .eq(Inventory::getWarehouseId,outbound.getWarehouseId())
                .eq(Inventory::getIsDeleted,0);
        Inventory one = iInventoryService.getOne(mpjLambdaWrapper);
        if(one.getStockQuantity()>=outbound.getOutboundQuantity()){
            one.setStockQuantity(one.getStockQuantity()-outbound.getOutboundQuantity());
            iInventoryService.updateById(one);
            outbound.setVersion(0);
            outboundMapper.insert(outbound);
        } else {
            if(one.getStockQuantity()<outbound.getOutboundQuantity()){
                throw new CustomException("8011","本仓库该物资库存不足，出库失败");
            }
        }
    }

    @Override
    public List<Outbound> export() {
        MPJLambdaWrapper<Outbound> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Outbound.class)
                .selectAs(Warehouse::getName,Outbound::getWarehouseName)
                .selectAs(Material::getName,Outbound::getMaterialName)
                .selectAs(Project::getName,Outbound::getProjectName)
                .innerJoin(Warehouse.class,Warehouse::getId,Outbound::getWarehouseId)
                .innerJoin(Material.class,Material::getId,Outbound::getMaterialId)
                .innerJoin((Project.class),Project::getId,Outbound::getProjectId);
        List<Outbound> list = outboundMapper.selectList(mpjLambdaWrapper);
        return list;
    }
}
